package com.ysd.lis.util.qcrule;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Data
public class RuleManager {


    public List<Rule> rules;

    public RuleManager()
    {
        rules = new ArrayList<>();
    }

    public void Register(RuleType ruleType, String id, String name, int a, int n, double l)
    {
//        Rule rule = ruleType switch
//        {
//            RuleType.ALRule => new ALRule(id, name, a, n, l),
//                RuleType.AOfNLRule => new AOfNLRule(id, name, a, n, l),
//                RuleType.ATRule => new ATRule(id, name, a, n, l),
//                RuleType.AXRule => new AXRule(id, name, a, n, l),
//                RuleType.RLRule => new RLRule(id, name, a, n, l),
//                _ => new Rule(id, name, a, n, l),
//        };
//        rules.add(rule);
            switch (ruleType.getRuleType()){
                case 1 :
                    rules.add(new RLRule(id, name, a, n, l)) ;
                    break;
                case 2 :
                    rules.add(new AXRule(id, name, a, n, l)) ;
                    break;
                case 3 :
                    rules.add(new ATRule(id, name, a, n, l)) ;
                    break;
                case 4 :
                    rules.add(new ALRule(id, name, a, n, l)) ;
                    break;
                case 5 :
                    rules.add(new AOfNLRule(id, name, a, n, l)) ;
                    break;

            }
    }


    public List<RuleType> Evaluate(QCResult result)
    {
        List<RuleType> resultList = new ArrayList<>();

        for(Rule rule : rules)
        {
            if( rule.Evaluate(result) == RuleStatement.OK)
            {
                resultList.add(rule.getType());
            }
        }

        //return resultList.Distinct().ToList();
        return resultList.stream().distinct().collect(Collectors.toList());
    }

    /// <summary>
    /// 多质控物验证,同时会验证单质控物
    /// </summary>
    /// <param name="results"></param>
    /// <returns></returns>
    public List<RuleType> EvaluateMulti( List<QCResult> results)
    {
        List<RuleType> resultList = new ArrayList<>();

        for (Rule rule : rules)
        {

            //单质控物验证时,仅验证主质控物
            //var result = results.FirstOrDefault(p => p.IsMainQCMaterial);
            QCResult result = results.stream().filter(p -> p.getIsMainQCMaterial()).findFirst().get();
            if (rule.Evaluate(result) == RuleStatement.OK)
            {
                resultList.add(rule.getType());
            }


            //for (int i =0;i< results.Count();i++)
            //{
            //    var result = results[i];
            //    if (rule.Evaluate(ref result) == RuleStatement.OK)
            //    {
            //        resultList.Add(rule.Type);
            //    }
            //}

            //验证多质控物
            if (rule.EvaluateMulti(results) == RuleStatement.OK)
            {
                resultList.add(rule.getType());
            }
        }

        //return resultList.Distinct().ToList();
        return resultList.stream().distinct().collect(Collectors.toList());
    }
}
